package com.kele.controller;


import com.kele.domain.Blog;
import com.kele.domain.ThumbsUp;
import com.kele.service.BlogService;
import com.kele.service.ThumbsUpService;
import com.kele.utils.ResponseData;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author 栗守佳
 */
@Controller
@RequestMapping("/thumbsUp")
public class ThumbsUpController {
    @Resource
    private ThumbsUpService thumbsupService;

    @Resource
    private BlogService blogService;

    /**
     * 点赞功能的实现
     * @param blogid
     * @param userid
     * @return
     */
    @RequestMapping(value = "/ThumbsUp",method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "博客id",name = "blogid",required = true,type = "int"),
            @ApiImplicitParam(value = "用户id",name = "userid",required = true,type = "int")
    })
    public ResponseData ThumbsUp(int blogid,int userid){
        //先查询点赞记录是否存在 ,然后在进行判断
        ThumbsUp ts=thumbsupService.findThumbsUp(blogid,userid);

        //判断集合的长度是否为null,就是判断是否点过赞
        if(ts!=null){
            //进行删除点赞记录
           int count= thumbsupService.deleteThumbsUp(ts.getThumbsupid());
            System.out.println("删除成功，删除的行数是"+count);
            Blog blog=blogService.findBlogById(blogid);
            //进行改变blog表中的数据,点赞数-1
            blogService.subThumbsUp(blog.getBid());
            return ResponseData.success("取消点赞成功",blog.getThumbsup()-1);
        }else {
            //根据id找到文章,然后对文章点赞数进行操作
            Blog blog = blogService.findBlogById(blogid);
            //thumbsup中建立新的点赞数据
            ThumbsUp thumbsUp=new ThumbsUp();
            thumbsUp.setBlogid(blogid);
            thumbsUp.setUserid(userid);
            Integer integer = thumbsupService.insertthumbsUp(thumbsUp);
            System.out.println("修改记录"+integer);
            //对文章点赞进行+1
           blogService.addThumbsUp(blog.getBid());
            return ResponseData.success("点赞成功",blog.getThumbsup()+1);
        }

    }

}
